package org.dromara.common.web.config;

import org.dromara.common.web.config.properties.XssProperties;
import org.dromara.common.web.filter.RepeatableFilter;
import org.dromara.common.web.filter.XssFilter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

/**
 * Filter配置
 *
 * @author Lion Li
 */
@AutoConfiguration
@EnableConfigurationProperties(XssProperties.class)
public class FilterConfig {

//    @Bean
//    @ConditionalOnProperty(value = "xss.enabled", havingValue = "true")
//    @FilterRegistration(
//        name = "xssFilter",
//        urlPatterns = "/*",
//        order = FilterRegistrationBean.HIGHEST_PRECEDENCE + 1,
//        dispatcherTypes = DispatcherType.REQUEST
//    )
//    public XssFilter xssFilter() {
//        return new XssFilter();
//    }


    @ConditionalOnProperty(value = "xss.enabled", havingValue = "true")
    @Bean(name = "xssFilter")
    public FilterRegistrationBean myFilter(){
        XssFilter myFilter = new XssFilter();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterRegistrationBean;
    }

    @Bean(name = "repeatableFilter")
    public FilterRegistrationBean repeatableFilter(){
        RepeatableFilter repeatableFilter = new RepeatableFilter();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(repeatableFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterRegistrationBean;
    }

}
